package org.shj.springcloud.zuul.service;

import org.shj.springcloud.zuul.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import rx.Observable;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


@Service
public class AggregationService {

	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod="fallback")
	public Observable<User> getUserById(String id){
		return Observable.create(observer -> {
			User user = restTemplate.getForObject("http://microservice-provider-user/{id}", User.class, id);
			observer.onNext(user);
			observer.onCompleted();
		});
	}
	
	@HystrixCommand(fallbackMethod="fallback")
	public Observable<User> getMovieUserByUserId(String id){
		return Observable.create(observer -> {
			User user = restTemplate.getForObject("http://microservice-consumer-movie-hystrix/user/{id}", User.class, id);
			observer.onNext(user);
			observer.onCompleted();
		});
	}
	
	public User fallback(String id){
		User user = new User();
		user.setId("no");
		user.setNickname("anyomous");
		return user;
	}
}
